#include <bits/stdc++.h>
using namespace std;

struct parity_game{
    struct { int l, r, ans; } query[110];
    int a[210], fa[210], d[210], n, m, t;
    void read_discrete() { // 读入、离散化
        cin >> n >> m;
        for (int i = 1; i <= m; i++) {
            char str[5];
            scanf("%d%d%s", &query[i].l, &query[i].r, str);
            query[i].ans = (str[0] == 'o' ? 1 : 0);
            a[++t] = query[i].l - 1;
            a[++t] = query[i].r;
        }
        sort(a + 1, a + t + 1);
        n = unique(a + 1, a + t + 1) - a - 1;
    }
    int get(int x) {
        if (x == fa[x]) return x;
        int root = get(fa[x]);
        d[x] ^= d[fa[x]];
        return fa[x] = root;
    }
    int main() {
        read_discrete();
        for (int i = 1; i <= n; i++) fa[i] = i;
        for (int i = 1; i <= m; i++) {
            // 求出l-1和r离散化之后的值
            int x = lower_bound(a + 1, a + n + 1, query[i].l - 1) - a;
            int y = lower_bound(a + 1, a + n + 1, query[i].r) - a;
            // 执行get函数，得到树根，并进行路径压缩
            int p = get(x), q = get(y);
            if (p == q) { // 已经在同一集合内
                if ((d[x] ^ d[y]) != query[i].ans) { // 矛盾，输出
                    cout << i - 1 << endl;
                    return 0;
                }
            }
            else { // 不在同一集合，合并
                fa[p] = q;
                d[p] = d[x] ^ d[y] ^ query[i].ans;
            }
        }
        cout << m << endl; // 没有矛盾
    }
};
struct supermarket{
    static const int N = 10006;
    int n, fa[N];
    pair<int, int> p[N];

    int get(int x) {
        if (x == fa[x]) return x;
        return fa[x] = get(fa[x]);
    }

    void Supermarket() {
        int d = 0, ans = 0;
        for (int i = 1; i <= n; i++) {
            cin >> p[i].first >> p[i].second;
            d = max(d, p[i].second);
        }
        for (int i = 0; i <= d; i++) fa[i] = i;
        sort(p + 1, p + n + 1);
        for (int i = n; i; i--) {
            int x = get(p[i].second);
            if (x) {
                ans += p[i].first;
                fa[x] = x - 1;
            }
        }
        cout << ans << endl;
    }

    int main() {
        while (cin >> n) Supermarket();
        return 0;
    }

};

int main(){
//    parity_game p;p.main();
    supermarket s;s.main();
}